

/**
 * 判断对象是否为字符串
 * @param {Object} obj
 */
export const isString = obj => {
    return (typeof obj === "string")
}

/**
 * 判断对象是否为数组
 * @param {Object} obj
 */
export const isArray = obj => {
    return (Array.isArray(obj))
}

/**
 * 判断对象是否为空
 * @param {Object} obj
 */
export const isEmptyObject = obj => {
    for (let pro in obj) { return false }
    return true
}

/**
 * 判断对象是否为null
 * @param {Object} obj
 */
export const isNullObject = obj => {
    return !obj
}

/**
 * 判断字符串是否为空
 * @param {String} obj
 */
export const isEmptyString = obj => {
    if (isNullObject(obj)) {
        return true
    }
    if (!isString(obj)) {
        return true
    }
    if (obj.length <= 0) {
        return true
    }
    return false
}

/**
 * 判断字符串是否为必填
 * @param {String} obj
 */
export const isMustProofString = obj => {
    if (isNullObject(obj)) {
        return true
    }
    if (!isString(obj)) {
        return true
    }
    if (obj.length <= 0) {
        return true
    }
    if (obj === "YES") {
        return true
    }
    return false
}

/**
 * 判断状态是否为成功， 以 SUCCESS 结尾
 * @param {String} obj
 */
export const isStatusSuccess = obj => {
    if (!isString(obj)) {
        return false
    }
    const flag = "SUCCESS"
    const temp = obj + ""
    return (temp.endsWith(flag))
}

/**
 * 判断是否为json
 * @param {String} obj
 */
export const isJsonString = obj => {
    try {
        JSON.parse(obj);
        return true;
    } catch (error) {
        return false;
    }
}

/**
 * 返回安全字符串
 * @param {String} obj
 */
export const toSafeString = obj => {
    if (isNullObject(obj)) {
        return ""
    }
    return obj
}

/**
 * 返回字符串列表，以指定格式分割
 * @param {String} obj
 */
export const toList = (obj, split) => {
    if (isNullObject(obj)) {
        return []
    }
    const splitFormat = isEmptyString(split) ? ":" : split
    return obj.split(splitFormat)
}

/**
 * 返回百分比字符串
 * @param {String} obj
 */
export const toPercentageString = (obj, fixed = 0) => {
    if (isNullObject(obj)) {
        return ""
    }
    const floatNumber = parseFloat(obj);
    const percentage = (floatNumber * 100).toFixed(fixed);
    return (percentage + "%")
}

/**
 * 返回去掉首尾空串的字符串
 * @param {String} obj
 */
export const toTrimString = (obj) => {
    if (isString(obj)) {
        const trimStr = obj.replace(/^\s+|\s+$/g, "");
        return trimStr
    }
    return obj
}


/**
 * 返回标签类型字符串
 * @param {String} obj
 */
export const toEventTagString = (obj) => {
    if (isString(obj)) {
        const trimStr = toTrimString(obj);
        const noSpaceStr = trimStr.replace(" ", "_").toUpperCase();
        return noSpaceStr
    }
    return obj
}

/**
 * 返回当前系统类型
 * @returns {String} 系统类型
 */
export const checkOSType = () => {
    const userAgent = navigator.userAgent.toLowerCase()
    // console.log("userAgent: " + userAgent)

    if (userAgent.indexOf("android") !== -1) {
        return "ANDROID"

    } else if ((userAgent.indexOf("iphone") !== -1) ||
        (userAgent.indexOf("ipad") !== -1) ||
        (userAgent.indexOf("ipod") !== -1) ||
        (userAgent.indexOf("ios") !== -1)) {
        return "IOS"

    } else {
        return "OTHER"
    }
}

/**
 * 格式化电话号码，防止被系统拦截
 * @returns {String} 电话号码
 */
export const callPhoneNumberFormat = (phoneNumber) => {
    return encodeURIComponent(phoneNumber);
}

/**
 * 提取纯数字
 * @returns {String} 文本内容
 */
export const extractNumbers = (text) => {
    const regex = /\b\d{4,}\b/g;
    const matches = text.match(regex);
    return matches ? matches : [];
}

/**
 * 格式化iso日期格式
 * @returns {String} iso日期
 */
export const formatISODate = (isoString) => {
    const date = new Date(isoString);

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');

    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

/**
 * 格式化时间毫秒日期格式
 * @param {number|string|Date} time 输入的时间，可以是毫秒数（13位或10位）、字符串或 Date 对象
 * @returns {string} 格式化后的日期字符串，格式为 "YYYY-MM-DD HH:mm:ss"
 */
export const formatMillsDate = (time) => {
    if (!time) {
        // throw new Error("Invalid input: time is required");
        return "null";
    }

    const timeF = Number(time);
    const milliseconds = String(time).length === 10 ? timeF * 1000 : timeF;
    const date = new Date(milliseconds);

    // let date;
    // if (time instanceof Date) {
    //     date = time;
    // } else if (typeof time === 'number' || /^\d+$/.test(time)) {
    //     const milliseconds = String(time).length === 10 ? time * 1000 : time;
    //     date = new Date(milliseconds);
    // } else if (typeof time === 'string') {
    //     date = new Date(time);
    // } else {
    //     throw new Error("Invalid input: time must be a number, string, or Date object");
    // }

    if (isNaN(date.getTime())) {
        // throw new Error("Invalid date");
        return "null";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};


/**
 * 当前日期格式-yyyy-MM-dd
 * @returns {String} yyyy-MM-dd
 */
export const simpleDate = () => {
    const date = new Date();

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');

    return `${year}-${month}-${day}`;
}